package com.lee.mysql.controller;

import com.github.pagehelper.PageInfo;
import com.lee.mysql.pojo.TableConfig;
import com.lee.mysql.service.TableConfigService;
import com.lee.mysql.service.TableDbInfoService;
import com.lee.mysql.vo.MenuVo;
import com.lee.mysql.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 表同步配置
 */

@RestController
@RequestMapping("/tableConfig")
@Api(tags = {"表同步配置"})
public class TableConfigController {
    @Autowired
    private TableConfigService tableConfigService;
    @Autowired
    private TableDbInfoService tableDbInfoService;



    /** 新增表同步配置 */
    @RequiresPermissions("system:tabConfig:add")
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    @ApiOperation(value = "新增表同步配置")
    public Result<String> addRecord(TableConfig record){

        int i = tableConfigService.addRecord(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"添加成功,请配置数据库信息!",null);
        }else {
            return new Result<>(Result.FAIL,"添加失败",null);
        }
    }

    /** 根据ID修改记录 */
    @RequiresPermissions("system:tabConfig:update")
    @RequestMapping(value = "/updateOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID修改记录")
    public Result<String> updateOneById(TableConfig record){
        TableConfig old = tableConfigService.queryOneById(record.getId());
        if ("1".equals(old.getConType()) && "2".equals(record.getConType())){
            int total = tableDbInfoService.getDbInfoTotal(record.getId());
            if (total > 2){
                return new Result<>(Result.FAIL,"原配置数据库信息条数超过[数据同步]类型条数[1对1]限制,不能改为[数据同步]类型",null);
            }
        }

        int i = tableConfigService.updateOneById(record);

        if (i > 0){
            return new Result<>(Result.SUCCESS,"修改成功",null);
        }else {
            return new Result<>(Result.FAIL,"修改失败",null);
        }
    }

    /** 根据ID批量删除记录 */
    @RequiresPermissions("system:tabConfig:delete")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID批量删除记录")
    @ApiImplicitParam(name = "ids", value = "id(单个,多个逗号隔开)", paramType = "query")
    public Result<String> deleteByIds(String ids){
        if (StringUtils.isBlank(ids)){
            return new Result<>(Result.FAIL,"请选择要删除的记录",null);
        }

        String[] split = ids.split(",");

        int i = tableConfigService.deleteByIds(Arrays.asList(split));

        if (i > 0){
            return new Result<>(Result.SUCCESS,"删除成功",null);
        }else {
            return new Result<>(Result.FAIL,"删除失败",null);
        }
    }

    /** 根据ID查询记录 */
    @RequestMapping(value = "/queryOneById", method = RequestMethod.POST)
    @ApiOperation(value = "根据ID查询记录")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    public Result<TableConfig> queryOneById(String id){

        TableConfig record = tableConfigService.queryOneById(id);

        return new Result<>(Result.SUCCESS,"操作成功",record);
    }


    /** 查询分页数据 */
    @RequiresPermissions("system:tabConfig:view")
    @RequestMapping(value = "/queryPaging", method = RequestMethod.POST)
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query")
    })
    public Result<PageInfo<TableConfig>> queryPaging(TableConfig record, Integer page, Integer size) {
        if (page == null) page = 1;
        if (size == null) size = 10;

        Result<PageInfo<TableConfig>> result = new Result<PageInfo<TableConfig>>();
        PageInfo<TableConfig> list = tableConfigService.queryPaging(record, page, size);

        result.setData(list);
        result.setErrorCode(Result.SUCCESS);
        return result;
    }


//    /** 获取所有启用配置 */
//    @RequestMapping(value = "/queryAllConfig", method = RequestMethod.POST)
//    @ApiOperation(value = "获取所有启用配置")
//    public Result<List<TableConfig>> queryAllConfig() {
//
//        List<TableConfig> list = tableConfigService.queryAllConfig();
//
//        return new Result<>(Result.SUCCESS,"操作成功",list);
//    }


    /** 获取所有启用配置列表 */
    @RequestMapping(value = "/queryAllConfigVo", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有启用配置列表")
    @ApiImplicitParam(name = "conType", value = "配置类型 1结构同步 2数据同步", paramType = "query")
    public Result<List<MenuVo>> queryAllConfigVo(String conType) {

        List<MenuVo> list = new ArrayList<>();
        List<TableConfig> list1 = tableConfigService.queryAllConfig(conType);
        list1.forEach(item->{
            MenuVo vo = new MenuVo();
            vo.setId(item.getId());
            vo.setTitle(item.getName());
            list.add(vo);
        });

        return new Result<>(Result.SUCCESS,"操作成功",list);
    }




}
